/**
 * @class Ext.app.ParametersPanel
 * @extends Ext.panel.Panel
 */
Ext.define('Ext.app.ParametersPanel', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.parameterPanel',
    border: false,
    id: 'parameterPanel',
    layout: 'form',
    bodyPadding: 10,
    items: [
        {xtype: 'text'}
    ],
    
    constructor: function (config) {
        this.dateFormat = config.dateFormat;
        this.dateTimeFormat = config.dateTimeFormat;
        
        this.callParent(arguments);
    },

    addParams: function(jsArray) {
        this.clearParams();
        jsArray.sort(function(a, b) {
            return (a.sortorder - b.sortorder);
        });
        var iLength = jsArray.length;
        for(var i=0; i<iLength; i++) {
            var field = {name: 'myfield' + i, code: jsArray[i].code, fieldLabel: jsArray[i].name, datatype: jsArray[i].datatype};
            var datatype = jsArray[i].datatype;
            if(datatype == "DATE") {
                field.xtype = "datefield";
                field.format = this.dateFormat;
            }
            else if(datatype == "DATETIME") {
                field.xtype = "datefield";
                field.format = this.dateFormat;
            }
            else if(datatype == "NUMBER") {
                field.xtype = "numberfield";
            }
            else if(datatype == "DECIMAL") {
                field.xtype = "numberfield";
            }
            else {
                field.xtype = "textfield";
            }
            this.add(field);
        }
    },
    
    clearParams: function() {
        this.removeAll();
    },
    
    getAllFields: function() {
        var cmpArray = this.queryBy(function(c) {
            return (c.name != null && (c.name.indexOf("myfield") == 0));
        });
        var iLength = cmpArray.length;
        var fields = [];
        for(var i=0; i<iLength; i++) {
            var f = {};
            f.fieldLabel = cmpArray[i].fieldLabel;
            f.code = cmpArray[i].code;
            f.name = cmpArray[i].name;
            f.xtype = cmpArray[i].xtype;
            f.format = cmpArray[i].format;
            f.value = this.getValue(cmpArray[i]);
            fields.push(f);
        }
        return fields;
    },
    
    addFields: function(fields) {
        this.clearParams();
        if(fields == null) {
            return;
        }
        var iLength = fields.length;
        for(var i=0; i<iLength; i++) {
            this.add(fields[i]);
        }
    },
    
    getValue: function(cmp) {
        var value = "";
        if(cmp.getXType() == "datefield") {
            value = cmp.getRawValue();
        }
        else {
            value = (cmp.getValue() == null ? "" : cmp.getValue());
        }
        return value;
    },
    
    getDataType: function(xtype) {
        if(xtype == "datefield") {
            return "DATE";
        }
        else if(xtype == "numberfield") {
            return "NUMBER";
        }
        return "STRING";
    },

    getData: function() {
        var cmpArray = this.queryBy(function(c) {
            return (c.name != null && (c.name.indexOf("myfield") == 0));
        });
        var iLength = cmpArray.length;
        var data = [];
        for(var i=0; i<iLength; i++) {
            var jsObj = {
                code: cmpArray[i].code, 
                value: this.getValue(cmpArray[i]),
                datatype: this.getDataType(cmpArray[i].xtype)
            };
            data.push(jsObj);
        }
        return data;
    }
});
